package Sort.面试.华为;

import org.junit.Test;

import java.util.*;

public class Distinct {

    public static Integer[] ARR = {1,2,3,4,5,5,6,6,7,7,8,8,9,9,41,41};

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        System.out.println("输入数组长度");
        int length = in.nextInt();
        int[] arr = new int[length];
        int index = 0;
        while (in.hasNextInt() && index < length) { // 注意 while 处理多个 case
            System.out.println("输入数组的值");
            int a = in.nextInt();
            arr[index] = a;
            index ++;
        }
//        int arr[] = new int[]{2,2,1};

        quickSort(arr,0, arr.length-1);

        int left = 0;
        int right = 1;
        System.out.println(arr[0]);
        while(right < arr.length){
            if(arr[left] != arr[right]){
                System.out.println(arr[right]);
                left = right;
                right = left + 1;
            }else{
                right ++;
            }
        }
    }

    public static void quickSort(int arr[], int start, int end){
        if(start > end){
            return;
        }
        int base = arr[start];
        int left = start;
        int right = end;

        while(left != right){
            while(arr[right] >= base && left < right){
                right--;
            }
            while(arr[left] <= base && left < right){
                left ++;
            }

            swap(arr, left, right);
        }

        arr[start] = arr[left];
        arr[left] = base;

        quickSort(arr, start, left -1);
        quickSort(arr, right + 1, end);

    }

    public static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }


}
